//
//  AvatarContent.swift
//  LeGou
//
//  Created by lxm on 2022/7/28.
//

import UIKit

class AvatarContent: UIView {

    //MARK: --------------------属性--------------------
    
    //MARK: --------------------UI控件--------------------
    let avatar = UIButton().then { b in
        b.setImage(UIImage.init(named: "def_user_img"), for: .normal)
        b.imageView?.contentMode = .scaleAspectFill
    }
    
    let hehuoren = ZMUtil.imageView(imageName: "hehuorenMark")
    
    let name = ZMUtil.label(title: "昵称", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 18, weight: .medium))
    
    let fwsIcon = ZMUtil.button(image: UIImage.init(named: "mine_fws"))
    
    let ckIcon = ZMUtil.button(image: UIImage.init(named: "level_ck_g"))
    
    let realNameIcon = ZMUtil.imageView(imageName: "mine_real")
    
    let levelStack = ZMUtil.stackView(axis: .horizontal, spacing: 4).then({$0.alignment = .center})
//        .then { v in
//        v.snp.makeConstraints { make in
//            make.width.equalTo(22)
//            make.height.equalTo(22)
//        }
//    }
    
    let tLevleIcon = ZMUtil.button(image: UIImage.init(named: "level_t_one_g"))
//        .then { v in
//        v.snp.makeConstraints { make in
//            make.width.equalTo(63)
//            make.height.equalTo(16)
//        }
//    }
    
    let inviteCode = ZMUtil.label(title: "推荐码：LM74899", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 12, weight: .regular))

    let copyBtn = ZMUtil.button(title: "复制", image: nil, font: UIFont.systemFont(ofSize: 10, weight: .medium), color: UIColor.zm.color333333())
    
    let wechatBtn = ZMUtil.button(title: "填写微信号", image: UIImage.init(named: "mine_wechat"), font: UIFont.systemFont(ofSize: 12, weight: .regular), color: UIColor.zm.color333333())
    
    let wdl = ZMUtil.label(title: "请登录", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 28, weight: .medium))

    let fwsAndRealNameStack = ZMUtil.stackView(axis: .horizontal, spacing: 8)

    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK: --------------------UI--------------------
    func setUI() {
        addSubview(avatar)
        avatar.layer.cornerRadius = 34
        avatar.layer.masksToBounds = true
        addSubview(name)
       
        addSubview(inviteCode)
        addSubview(copyBtn)
        
        addSubview(fwsAndRealNameStack)
        fwsAndRealNameStack.alignment = .center
//        fwsAndRealNameStack.addArrangedSubview(tLevleIcon)
        fwsAndRealNameStack.addArrangedSubview(realNameIcon)
        
        levelStack.addArrangedSubview(ckIcon)

        levelStack.addArrangedSubview(fwsIcon)
        
        levelStack.addArrangedSubview(tLevleIcon)

        
        addSubview(levelStack)
        
//        fwsAndRealNameStack.distribution = .fill

        
//        tLevleIcon.snp.makeConstraints { make in
//            make.left.right.equalToSuperview()
//        }
        
        copyBtn.contentEdgeInsets = UIEdgeInsets(top: 2, left: 6, bottom: 2, right: 6)
        copyBtn.sizeToFit()
        copyBtn.layer.cornerRadius = copyBtn.zm.height / 2
        copyBtn.backgroundColor = "#E1E1E1".zm.hex()
        addSubview(wechatBtn)
        wechatBtn.contentEdgeInsets = UIEdgeInsets(top: 6, left: 12, bottom: 6, right: 12)
        wechatBtn.sizeToFit()
        wechatBtn.layer.cornerRadius = wechatBtn.zm.height / 2
        wechatBtn.backgroundColor = .white
        
        addSubview(wdl)
        
        let tap = UITapGestureRecognizer()
        tap.addTarget(self, action: #selector(onTapLogin))
        wdl.isUserInteractionEnabled = true
        wdl.addGestureRecognizer(tap)
        
        name.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        name.setContentHuggingPriority(.defaultLow, for: .horizontal)
        
        addSubview(hehuoren)
    }
    
    @objc func onTapLogin() {
        if LoginService.shared.checkLogin() == false {return}
    }
    
    func addConstraints() {
        avatar.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(68)
        }
        
        name.snp.makeConstraints { make in
            make.top.equalTo(avatar.snp.top).offset(0)
            make.left.equalTo(avatar.snp.right).offset(16)
        }
        
//        fwsIcon.snp.makeConstraints { make in
//            make.centerY.equalTo(name)
//            make.left.equalTo(name.snp.right).offset(8)
//        }
        
        levelStack.snp.makeConstraints { make in
            make.leading.equalTo(name)
            make.top.equalTo(name.snp.bottom).offset(4)
        }
        
        realNameIcon.snp.makeConstraints { make in
            make.width.height.equalTo(22)
        }
        
        fwsAndRealNameStack.snp.makeConstraints { make in
            make.centerY.equalTo(name)
            make.left.equalTo(name.snp.right).offset(8)
            make.right.lessThanOrEqualToSuperview().offset(-5)
        }
        
        inviteCode.snp.makeConstraints { make in
            make.leading.equalTo(name)
            make.centerY.equalTo(wechatBtn)
        }
        
        copyBtn.snp.makeConstraints { make in
            make.centerY.equalTo(wechatBtn)
            make.left.equalTo(inviteCode.snp.right).offset(8)
        }
        
        wechatBtn.snp.makeConstraints { make in
            make.top.equalTo(levelStack.snp.bottom).offset(9)
            make.right.lessThanOrEqualToSuperview().offset(-16)
            make.left.equalTo(copyBtn.snp.right).offset(15)
//            make.bottom.equalToSuperview()
        }

        wdl.snp.makeConstraints { make in
            make.centerY.equalTo(avatar)
            make.left.equalTo(avatar.snp.right).offset(16)
        }
        
        hehuoren.snp.makeConstraints { make in
            make.bottom.equalTo(avatar.snp.bottom)
            make.centerX.equalTo(avatar)
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        
    }
    //MARK: --------------------delegate--------------------

}
